﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasFamilyHoodDAL : IMasFamilyHoodDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasFamilyHoodDAL()
        {

        }
        public DataSet LoadFamilyHoods(MasFamilyHoodInfo famInfo)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", famInfo.Sequence);

                strSQL = "SELECT FAM_SEQ, FAM_NAME, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_FAMILYHOOD"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (famInfo.Sequence > 0 ? " AND FAM_SEQ = @Sequence" : "")
                    + " ORDER BY FAM_NAME";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
        public IList<MasFamilyHoodInfo> ListFamilyHoods(MasFamilyHoodInfo famInfo)
        {
            string strSQL;

            List<MasFamilyHoodInfo> infoList = new List<MasFamilyHoodInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", famInfo.Sequence);

                strSQL = "SELECT FAM_SEQ, FAM_NAME, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_FAMILYHOOD"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (famInfo.Sequence > 0 ? " AND FAM_SEQ = @Sequence" : "")
                    + " ORDER BY FAM_NAME";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasFamilyHoodInfo Info = new MasFamilyHoodInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? (int?)null : rdr.GetInt32(3)
                            , rdr.IsDBNull(4) ? (int?)null : rdr.GetInt32(4));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return infoList;
        }
        public void Insert(MasFamilyHoodInfo Info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Name", Info.Name);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(2, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(3, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_FAMILYHOOD(FAM_NAME, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Name, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(MasFamilyHoodInfo Info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Name", Info.Name);
                dbManager.AddParameters(1, "@Sequence", Info.Sequence);
                dbManager.AddParameters(2, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_FAMILYHOOD"
                    + " SET FAM_NAME = @Name"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE FAM_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_FAMILYHOOD"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE FAM_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
